<?php
header('Content-Type:text/html;charset=utf-8');
/**
 * 处理用户登陆和注册业务逻辑
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:xjh
 * Date:2016-2-16
*/
class UserController extends Controller{

	/**
	 * 自定义验证码
	 */
	function actionVerifyCode(){
		//设置4个验证码数量
		$_code = new code(4);
		$a = $_code->start();
		Yii::app()->session['session_code'] = $_code->getCode();//写入session
		$this->render('register',array('verifycode'=>$a));
	}
	/**
	 * 验证码,以方法的形式访问其他类
	 * Author:xjh
	 * Date:2016-2-28
	 * 暂时不用这个方法了
	 */
	function actions(){
		return array(
			'captcha' => array(//相当于./index.php?r=user/captcha
				'class' => 'system.web.widgets.captcha.CCaptchaAction',//路径引入类，路径的别名,不建议，因为会修复原框架bug
//				'class' => 'application.components.Captcha',//路径引入类，路径的别名,不建议，因为会修复原框架bug
			'backColor' => 0xffffff, 
			'width' => 121,
			'height' => 47,
			'maxLength'=>4,
			'minLength'=>4,
			'offset' => 3,
			'transparent'=>true,
			'testLimit'=>0,//限制相同验证码出现的次数
			),
		);
	}
	
	/**
	 * actionLogin 用户控制器的登陆方法
	 * Author:xjh
	 * Date:2016-2-28
	 */
	function actionLogin(){
		//创建登陆模型对象
		$user_login = new LoginForm;
		if(isset($_POST['LoginForm'])){
			$user_login->attributes = $_POST['LoginForm'];
			/**
			 * 前端+数据库有效性校验,跳转,持久化信息存储(Cookie)
			 */
			if($user_login -> validate() && $user_login->login()){
			//$user_login->_success("登陆成功","./index.php?r=index/index");
//				$this->redirect("./index.php?r=index/index");
				echo '<script>history.go(-2);</script>';
			}else{
				$user_login->_error("登陆失败");
			}
		}
		$this->renderPartial('login',array('user_login' => $user_login));
	}
	/**
	 * actionLogin 登陆退出
	 * Author:xjh
	 * Date:2016-2-27
	 */
	function actionLogout(){
//		Yii::app()->session->clear();//删除内存里session变量信息
//		Yii::app()->session->destroy();//删除服务器的session文件
		Yii::app()->user->logout();//同时删除cookie和session
		$this->redirect('./index.php?r=index/index');
	}
	/**
	 * actionRegister 用户控制器的注册方法
	 * Author:xjh
	 * Date:2016-2-27
	 */
	function actionRegister(){

		$_SESSION['last_access'] = time();
		//		echo Yii::app()->session['session_code'];
		//添加与查询不同，要实例化对象
		$user_model = new User();
		if(isset($_POST['type']) && $_POST['type'] == 'input_data'){
			$_result = array(0,1);
			if ($_POST['I']['code'] != Yii::app()->session['session_code']){
				echo json_encode($_result[0]);
			}else{
				echo json_encode($_result[1]);
			}
			exit();
		}
		if(isset($_POST['User'])){
			 	//采用单独的验证码进行验证
				if(Yii::app()->session['session_code'] != $_POST['code']){
//				 	$user_model->_error("验证码错误");
//				 	exit;
				}
				if(!empty($_POST['User']['user_hobby']))
					$_POST['User']['user_hobby'] = implode(',',$_POST['User']['user_hobby']);
				/**
				 *调用__set()直接写入属性
				 */
				$_POST['User']['user_password'] = sha1($_POST['User']['user_password']);
				$_POST['User']['user_password_confirm'] = sha1($_POST['User']['user_password_confirm']);
				$user_model->attributes = $_POST['User'];
				$user_model->user_register = date("Y/m/d H:i:s",time());
				$user_model->user_last_time = date("Y/m/d H:i:s",time());
				$user_model->user_last_ip = Yii::app()->request->userHostAddress;
				//存入数据库
				if($user_model->save()){
					$user_model->_success("注册成功",'./index.php?r=index/index');
				}else{ 
					$user_model->_error("注册失败,请查看提示信息");
				}
		}
		//集合封装
		$cmodel = new CCCModel();
		$role_info = $cmodel -> sel("SELECT role_id,role_name FROM {{role}} ");
		foreach ($role_info as $_k => $_v){
			if($_v['role_name'] == "管理员") continue;
			$role_result[$_v['role_id']] = $_v['role_name'];	
		}
		$question = array(
			'1' => '',
			'2' => '你最喜欢的男明星',
			'3' => '你最喜欢的动漫',
			'4' => '你最喜欢的电影',
			'5' => '你最喜欢的动物'
		);
		
		$sex = array(
			'1' => '男',
			'2' => '女',
			'3' => '保密'
		);
		
		$job = array(
			'1' => '',
			'2' => '政府单位',
			'3' => '事业单位',
			'4' => '个人私企',
			'5' => '学生',
			'6' => '无业'
		);
		$hobby = array(
			'1' => '篮球',
			'2' => '乒乓球',
			'3' => '网球',
			'4' => '游泳',
			'5' => '长跑',
			'6' => '其他',
		);
		$this->render('register',array('user_model'=>$user_model,'sex'=>$sex,'job'=>$job,'hobby'=>$hobby,'question'=>$question,'role_id'=>$role_result));
	}
	
	
	function actionUserInfo(){
 		$this->layout='//layouts/order';
		$addr_model = new Addr();
		$infos = $addr_model -> getAddrByUserId(Yii::app()->user->id);
		$user_info = $addr_model -> geUserByUserId(Yii::app()->user->id);
		$user_info = $user_info[0];
		$active = array(
			0 => '否',
			1 => '是'
		);
		$this->render('userinfo',array('addr_infos'=>$infos,'active'=>$active,'user_info'=>$user_info));
	}
	
	//测试方法
	function actionaaa(){
		
		$user_model = new User();
		
//		$row = $user_model -> checkPassword(Yii::app()->user->id,sha1($_POST['r_password']));

		echo 1;
	}
	/**
	 * 用户自行修改密码
	 */
	function actionupdPassword(){
	
		$user_model = new User();
		if($_POST){
			$row = $user_model -> checkPassword(Yii::app()->user->id,sha1($_POST['r_password']));
			if(!empty($row)){
				if($_POST['n_password'] == $_POST['n_confirm_password']){
					$row = $user_model -> updPassword(Yii::app()->user->id);
					if($row){
						echo 1;//修改成功
						exit;
					}else{
						echo 2;//密码与原来一致
						exit;
					}
				}else{
					    echo 3;//两次密码不一致
					    exit;
				}
			}else{
				echo 0;//原密码错误
				exit;
			}
		}
		
	}

	/**
	 * 用户地址增加功能
	 */
	function actionaddr(){
		$addr_model = new Addr();
		if(isset($_POST) && $_POST['send'] == '保存'){
		 $row = $addr_model -> setAddr(Yii::app()->user->id);
		 if($row){
 			$this->layout='//layouts/order';
			$this->redirect('./index.php?r=user/userinfo');
		 }else{
		 	echo "操作失败，请返回继续操作";
		 	exit;
		 }
		}
	}
	
	/**
	 * 用户修改地址
	 */
	function actionaddrupd($addr_id){
		$this->layout='//layouts/order';
		$addr_model = new Addr();
		$infos = $addr_model -> getAddrByUserId(Yii::app()->user->id);
		
		if(@$_POST['send'] == '保存'){
			$row = $addr_model -> updAddr($addr_id);
			if($row){
				$this->redirect('./index.php?r=user/userinfo');
			}else{
				echo "操作失败,请返回";
				exit;
			}
		}
		
		$updValues = $addr_model -> getAddrByAddrId($addr_id);
		$updValues = $updValues[0];
		$active = array(
			0 => '否',
			1 => '是'
		);
		$this->render('userinfo',array('addr_infos'=>$infos,'active'=>$active,'updValues'=>$updValues));
		
	}
	
	/**
	 * 短信验证
	 */
	public function actionyzm($tel){
		
		session_start();
		$_SESSION['code'] = $this->actionyzmyzm();
		
		if(!isset($_SESSION['last_access']) ||(time()-$_SESSION['last_access']) > 60 ){
			$_SESSION['last_access'] = time();
			unset($_SESSION['code']);
		}else{
//			$_SESSION['code'] = $this->actionyzmyzm();
		}
		$sms=new ChuanglanSMS('N1936991','b93da3c6');
		$result=$sms->send($tel,"【sss】您的验证码是".$_SESSION['code']);
	}
	
	
	//判断手机验证码状态
	 function actioncheckTelCode(){
		session_start();
		if($_SESSION['code'] == $_POST['yzm']){
			echo 1;
		}else{
			echo 0;
		}
	}
	
	function actionyzmyzm(){
		$_SESSION['last_access'] = time();
		$acode = '';
		for ($i = 0; $i < 4; $i++) {
			$acode .= rand(0,9);
		}
		return $acode;
	}

}
?>